package com.example.demo.loop.service;

import com.example.demo.loop.comon.MyException;
import com.example.demo.loop.domain.*;
import com.example.demo.loop.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: daiguoqing
 * @Date: 2020-08-31
 * @Time: 16:00
 * @Version: hello-spring-boot 1.0
 */
@Service
public class StudentMassage {

    //注入service类
//    @SuppressWarnings("all")
    @Autowired
    public StudentMapper studentMapper;

    //自定义错误异常
   /* @Autowired
    ServiceException exception;*/

    //查询班级信息
    public List<Grade> selectGrade(){
        List<Grade> list = studentMapper.selectGrade();
        return list;
    }
    //查询学科信息
    public List<Elective> selectElective(){
        List<Elective> list = studentMapper.selectElective();
        return list;
    }

    //查询学科id
    public long elName(Elective elective){
        long eid = studentMapper.elName(elective.getName());
        return eid;
    }

    //查询班级id
    public long grName(Grade grade){
        long gid =  studentMapper.elName(grade.getName());
        return gid;
    }


    //查询班级名称
    public String SelectGradeName(){
        String GradeName = studentMapper.SelectGradeName();
        return GradeName;
    }

    //查询班级名称带参数
    public String SelectGradeNameA(long gid){
        String GradeName = studentMapper.SelectGradeNameA(gid);
        return GradeName;
    }

    //查询学科名称带参数
    public String SelectElectiveNameA(long eid){
        String ElectiveName = studentMapper.SelectElectiveNameA(eid);
        return ElectiveName;
    }

    //查询学科名称
    public String SelectElectiveName(){
        String ElectiveName = studentMapper.SelectElectiveName();
        return ElectiveName;
    }

    //新增
    //@Transactional
    public void AddUser(User user) {
        UserDto userDto = new UserDto();
        //保存学生姓名
        userDto.setName(user.getName());
        //保存性别
        if(user.getSex().equals("男")){
            userDto.setSex("1");
        }else if(user.getSex().equals("女")){
            userDto.setSex("2");
        }
        //保存手机号
        userDto.setPhone(user.getPhone());
        //判断班级id
        if(user.getGName().equals(user.getGName())){
            userDto.setGid(studentMapper.grName(user.getGName()));
        }
        //判断学科id
        if(user.getEName().equals(user.getEName())){
            userDto.setEid(studentMapper.elName(user.getEName()));//学科id
        }
        //保存学生地址
        userDto.setAddress(user.getAddress());
        //保存userDto对象
        studentMapper.AddUser(userDto);
    }
    //删除
    public void deleteUser(long id) {
        studentMapper.deleteUser(id);
    }

    //查询全部学生信息
    public List<User> UserAll(String stusName, Long gsName){
        /*if(stusName==null||gsName==null){
            throw new MyException(401,"学生姓名不能为空或者班级不能为空！");
        }*/
        List<User> userList = studentMapper.UserAll( stusName,  gsName);
        return userList;
    }


    //根据条件查询   姓名  班级
    public List<User> selectUser(String stusName, Long gsName) {

        List<UserDto> userDtoList = studentMapper.selectUser(stusName, gsName);
        List<User> userArrayList = new ArrayList<>();
        for (UserDto userDto : userDtoList) {
            User user = new User();
            user.setId(userDto.getId());
            user.setName(userDto.getName());
            user.setSex(userDto.getSex());
            user.setPhone(userDto.getPhone());
            user.setAddress(userDto.getAddress());
            user.setEName(studentMapper.SelectElectiveNameA(userDto.getEid()));
            user.setGName(studentMapper.SelectGradeNameA(userDto.getGid()));
            userArrayList.add(user);
        }
        return userArrayList;
    }

    //通过前端端ajax传入的id,查询学生信息,回显到模态框中
    public User selectUserDto(long id){
      /*  if(id==0){
            throw new MyException(401,"用户id未找到！");
        }*/
        User user = studentMapper.selectUserDto(id);
        if(user.getSex().equals("1")){
            user.setSex("男");
        }else if(user.getSex().equals("2")){
            user.setSex("女");
        }
        return user;
    }

    //修改用户信息
    @Transactional
    public void updateUser(User user) {

        long l1 = Long.parseLong(user.getEName());
        long l2 = Long.parseLong(user.getGName());

        UserDto userDto = new UserDto();
        userDto.setGid(l2);
        userDto.setEid(l1);
        userDto.setName(user.getName());
        userDto.setPhone(user.getPhone());
        userDto.setId(user.getId());
        userDto.setSex(user.getSex());
        userDto.setAddress(user.getAddress());
        studentMapper.updateUser(userDto);
    }

    //查询全部学生信息
    public List<User> selectUserAll(){
        List<User> userList = studentMapper.selectUserAll();
        return userList;
    }

      /*UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setName(user.getName());
        userDto.setPhone(user.getPhone());
        userDto.setAddress(user.getAddress());
        //保存性别
        if(user.getSex().equals("男")){
            userDto.setSex("1");
        }else if(user.getSex().equals("女")){
            userDto.setSex("2");
        }
        //添加班级id
        if(user.getGName().equals(studentMapper.grName(user.getGName()))){
            Long gradeId = studentMapper.grName(user.getGName());
            userDto.setGid(gradeId);
        }
        if(user.getEName().equals(studentMapper.elName(user.getEName()))){
            //判断学科id
            userDto.setEid(studentMapper.elName(user.getEName()));//学科id
            studentMapper.updateUser(userDto);
        }*/

  /*  @Transactional
    public boolean addStudents(){
        UserDto userDto=new UserDto();
        userDto.setAddress("山东济南");
        userDto.setPhone(15315112033L);
        userDto.setName("张三");
        userDto.setSex("男");
        if(studentMapper.AddUser(userDto)>0){
            userDto.setName("张三111");
            if(studentMapper.AddUser(userDto)>0){
                return true;
            }else{
                throw new RuntimeException("新增第二条用户失败！！");
            }
        }
        return false;
    }*/
}
